const md5 = require('md5');
const fs = require('fs');
const path = require('path');
var milt = require('../lib/upload');
// var path = require('path'); 
function index(router) {
	router.post('/getlist', async (ctx) => {
		var param = ctx.request.body;
		var list = require('../data/' + param.uid + '.json');
		ctx.response.body = {
			code: 200,
			info: 'OK',
			lst: list
		};
	});
	router.post('/addflist', async (ctx) => {
		var root = ctx.root;
		var param = ctx.request.body;
		try {
			var list = require(root + '/data/' + param.uid + '.json');
			// console.log(list)
			var data = await ctx.db.get({
				dbname: "mmuser",
				field: "*",
				where: { userId: param.sid }
			});
			list.push(data[0]);
			try {
				fs.unlinkSync(root + '/data/' + param.uid + '.json');
			} catch (e) {
				console.log('文件不存在')
			}
			fs.writeFile(root + '/data/' + param.uid + '.json', JSON.stringify(list), { 'flag': 'a' }, function (err) {
				if (err) {
					throw err;
				}
			});

			var lists = require(root + '/data/' + param.sid + '.json');
			// console.log(list)
			var datas = await ctx.db.get({
				dbname: "mmuser",
				field: "*",
				where: { userId: param.uid }
			});
			lists.push(datas[0]);
			try {
				fs.unlinkSync(root + '/data/' + param.sid + '.json');
			} catch (e) {
				console.log('文件不存在')
			}
			fs.writeFile(root + '/data/' + param.sid + '.json', JSON.stringify(lists), { 'flag': 'a' }, function (err) {
				if (err) {
					throw err;
				}
			});

			ctx.response.body = {
				code: 200,
				info: 'OK',
				lst: list
			};
		} catch (error) {
			ctx.response.body = {
				code: 400,
				info: '服务器错误',
				lst: null
			};
		}

	});
	router.post('/getsolist', async (ctx) => {
		var param = ctx.request.body;
		// console.log("SELECT * FROM mmuser WHERE wechatNumber REGEXP "+param.str)
		var data = await ctx.db.query("SELECT * FROM mmuser WHERE wechatNumber REGEXP '" + param.str + "' or userName REGEXP '" + param.str + "' limit 20");
		ctx.response.body = {
			code: 200,
			info: 'OK',
			lst: data
		};
	});
	router.post('/setInsave', async (ctx) => {//临时保存文件
		var param = ctx.request.body;
		const files = ctx.request.files.file;
		// console.log(files)
		// const name = ctx.request.name;
		var ip = ctx.win.ip;
		if (ip.indexOf('http') == -1) {
			ip = 'http://' + ip;
		};
		var da = new Date();
		var saveda = new Date(da.getTime() + 24 * 60 * 60 * 1000 * 3).getTime();
		// console.log(saveda)
		var name = ip + ':8888/setIndir/' + (milt([files], './static/setIndir', saveda).name).replace();
		ctx.response.body = {
			code: 200,
			info: 'OK',
			lst: name
		};
	});

	router.get('/getsoflist', async (ctx) => {//获取所有用户朋友圈内容
		var param = ctx.request.query;
		var page = param.page ? param.page : 1;//页码
		var limit = param.limit ? param.limit : 20;//条数
		var id = param.uid;
		var str = "(SELECT * FROM " + id + "name) UNION "; //"(SELECT * FROM 5name ) UNION(SELECT * FROM 4name) ORDER BY time desc limit 1";
		var flist = require('../data/' + id + '.json');//用户好友列表
		for (var i in flist) {
			var o = flist[i].userId;
			if (i != flist.length - 1) {
				str += "(SELECT * FROM " + o + "name) UNION ";
			} else {
				str += "(SELECT * FROM " + o + "name) ";
			};
		};
		str = str + "ORDER BY time desc limit " + (limit * page);
		// console.log(str)
		var dbdata = await ctx.db.query(str);
		// console.log(dbdata,123)
		ctx.response.body = {
			code: 200,
			info: 'OK',
			lst: dbdata
		}
	});
	router.get('/getmylist', async (ctx) => {//获取当前朋友圈内容
		var param = ctx.request.query;
		var page = param.page ? param.page : 1;
		var limit = param.limit ? param.limit : 20;
		var id = param.uid;
		var str = "SELECT * FROM " + id + "name ORDER BY time desc limit " + (limit * page);
		if (param.id) {
			str = "SELECT * FROM " + id + "name where(id=" + param.id + ") ORDER BY time desc limit " + (limit * page);
		};
		var dbdata = await ctx.db.query(str);
		ctx.response.body = {
			code: 200,
			info: 'OK',
			lst: dbdata
		}
	});
}
module.exports = index;
